<template>
  <div class="api-key-config">
    <button @click="showDialog = true" class="config-button">
      <span class="icon">🔑</span>
      配置 API 密钥
    </button>

    <div v-if="showDialog" class="config-dialog">
      <div class="dialog-content">
        <div class="dialog-header">
          <h3>API 密钥配置</h3>
          <button @click="showDialog = false" class="close-button">×</button>
        </div>
        
        <div class="dialog-body">
          <p class="description">
            请输入您的 API 密钥，用于访问大模型服务。
            <br>
            <small>密钥将保存在浏览器本地存储中，不会发送到其他服务器。</small>
          </p>
          
          <div class="form-group">
            <label for="api-key">API 密钥:</label>
            <div class="input-group">
              <input 
                :type="showKey ? 'text' : 'password'"
                id="api-key"
                v-model="apiKeyInput"
                placeholder="输入您的 API 密钥"
              />
              <button 
                @click="toggleShowKey" 
                class="toggle-button"
                type="button"
              >
                {{ showKey ? '隐藏' : '显示' }}
              </button>
            </div>
          </div>
        </div>
        
        <div class="dialog-footer">
          <button @click="showDialog = false" class="cancel-button">取消</button>
          <button @click="saveApiKey" class="save-button">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'

const props = defineProps<{
  modelValue: string
}>()

const emit = defineEmits<{
  (e: 'update:modelValue', value: string): void
}>()

const apiKeyInput = ref(props.modelValue)
const showDialog = ref(false)
const showKey = ref(false)

// 从本地存储加载 API 密钥
onMounted(() => {
  const savedApiKey = localStorage.getItem('api_key')
  if (savedApiKey) {
    apiKeyInput.value = savedApiKey
    emit('update:modelValue', savedApiKey)
  }
})

// 切换显示/隐藏密钥
const toggleShowKey = () => {
  showKey.value = !showKey.value
}

// 保存 API 密钥
const saveApiKey = () => {
  const apiKey = apiKeyInput.value.trim()
  if (apiKey) {
    localStorage.setItem('api_key', apiKey)
    emit('update:modelValue', apiKey)
  }
  showDialog.value = false
}
</script>

<style scoped>
.api-key-config {
  position: relative;
}

.config-button {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background: #f0f0f0;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  color: #555;
  transition: all 0.2s;
}

.config-button:hover {
  background: #e8e8e8;
  color: #333;
}

.icon {
  font-size: 1.1rem;
}

.config-dialog {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog-content {
  background: white;
  border-radius: 8px;
  width: 90%;
  max-width: 500px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  overflow: hidden;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid #eee;
}

.dialog-header h3 {
  margin: 0;
  color: #333;
  font-size: 1.2rem;
}

.close-button {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #999;
  padding: 0;
  line-height: 1;
}

.close-button:hover {
  color: #333;
}

.dialog-body {
  padding: 1.5rem;
}

.description {
  margin-top: 0;
  margin-bottom: 1.5rem;
  color: #666;
  line-height: 1.5;
}

.description small {
  color: #888;
  font-size: 0.85rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 500;
  color: #555;
}

.input-group {
  display: flex;
  width: 100%;
}

.input-group input {
  flex: 1;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-right: none;
  border-radius: 4px 0 0 4px;
  font-size: 1rem;
}

.input-group input:focus {
  outline: none;
  border-color: #007bff;
}

.toggle-button {
  padding: 0 1rem;
  background: #f0f0f0;
  border: 1px solid #ddd;
  border-radius: 0 4px 4px 0;
  cursor: pointer;
  color: #555;
}

.toggle-button:hover {
  background: #e8e8e8;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  padding: 1rem 1.5rem;
  border-top: 1px solid #eee;
  gap: 1rem;
}

.cancel-button {
  padding: 0.6rem 1.2rem;
  background: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  color: #555;
}

.cancel-button:hover {
  background: #e8e8e8;
}

.save-button {
  padding: 0.6rem 1.2rem;
  background: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.save-button:hover {
  background: #0069d9;
}
</style>